package com.example.backend.service.serviceImpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.entity.BookletVO;
import com.example.backend.entity.User;
import com.example.backend.entity.UserVO;
import com.example.backend.mapper.UserBookletMapper;
import com.example.backend.mapper.UserMapper;
import com.example.backend.mapper.UserPostMapper;
import com.example.backend.mapper.UserRoleMapper;
import com.example.backend.service.AdminUserService;
import com.example.backend.service.BookletService;
import com.example.backend.service.PostService;
import com.example.backend.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Jcy
 * @Date 2022/5/6
 */
@Component
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserBookletMapper userBookletMapper;
    @Autowired
    private BookletService bookletService;
    @Autowired
    private UserPostMapper userPostMapper;
    @Autowired
    private PostService postService;

    @Override
    public PageUtils getUsers(int page, int pageSize){
        Page<UserVO> p = new Page<>(page, pageSize);
        p.setRecords(this.userMapper.getUser(p));
        return new PageUtils(p);
    }
    @Override
    public Map<String, Object> getUserInformation(String phoneNumber, int userId){
        if((phoneNumber==null&&userId!=0)||(phoneNumber!=null&&userId!=0)){
            User user = userMapper.selectById(userId);
            Map<String, Object> map = new HashMap<>();
            if(userMapper.selectById(userId)==null){
                return null;
            }
            map.put("phoneNumber",user.getPhone_number());
            map.put("password",user.getPassword());
            map.put("userName",user.getNickname());
            map.put("sex",user.getSex());
            if(user.getBirthday()==null){
                map.put("birthday", "");
            }
            else {
                map.put("birthday", user.getBirthday().toString());
            }
            map.put("education",user.getEducation());
            map.put("school",user.getSchool());
            map.put("career",user.getCareer());
            return map;
        }
        else if(phoneNumber!=null){

            Map<String, Object> map1 = new HashMap<>();
            User user1 = userMapper.getInformation(phoneNumber);
            if(userMapper.getInformation(phoneNumber)==null){
                return null;
            }
            map1.put("phoneNumber",user1.getPhone_number());
            map1.put("password",user1.getPassword());
            map1.put("userName",user1.getNickname());
            map1.put("sex",user1.getSex());
            if(user1.getBirthday()==null){
                map1.put("birthday", "");
            }
            else {
                map1.put("birthday", user1.getBirthday().toString());
            }
            map1.put("education",user1.getEducation());
            map1.put("school",user1.getSchool());
            map1.put("career",user1.getCareer());
            return map1;
        }
        return null;
    }
    @Override
    public int modifyUser(int userId,Map<String, Object> params){
        if(userMapper.selectById(userId)==null){
            return 1;
        }
        else {
            User user = userMapper.selectById(userId);
            String password = (String) params.get("password");
            String nickname = (String) params.get("userName");
            String sex = (String) params.get("sex");
            if(params.get("birthday")==null){
                user.setBirthday(null);
            }
            else if(params.get("birthday")!=null) {
                Date birthday = Date.valueOf((String) params.get("birthday"));
                user.setBirthday(birthday);
            }
            String education = (String) params.get("education");
            String school = (String) params.get("school");
            String career = (String) params.get("career");
            user.setPassword(password);
            user.setNickname(nickname);
            user.setSex(sex);
            user.setEducation(education);
            user.setSchool(school);
            user.setCareer(career);
            userMapper.updateById(user);
            return 0;
        }
    }
    @Override
    public int deleteUser(int userId,Map<String, Object> params){
        int adminId = (int) params.get("adminId");
        String adminPassword = (String) params.get("adminPassword");
        if(userMapper.selectById(userId)==null){
            return 1;
        }
        else{
            if(userMapper.selectById(adminId)==null) {
                return 2;
            }
            else if(userRoleMapper.getUserRole(adminId)==2){
                return 3;
            }
            else if(userMapper.selectById(adminId)!=null&&userRoleMapper.getUserRole(adminId)==1){
                User user = userMapper.selectById(adminId);
                if(!adminPassword.equals(user.getPassword())){
                    return 4;
                }
                if(adminPassword.equals(user.getPassword())){
                    userMapper.deleteById(userId);
                    if(userBookletMapper.isBooklet(userId)!=null){
                        bookletService.deleteBooklet(adminId, userBookletMapper.getBookletId(userId));
                    }
                    if(userPostMapper.isPost(userId)!=null){
                        postService.deletePost(userPostMapper.getPostId(userId));
                    }
                    return 0;
                }
            }
               return 0;
        }
    }
}
